class Solution {
public:
    string compressString(string S) {
        string ret;
        int left = 0, right = 0;
        int n = S.length();
        while (right < n) {
            if (S[right] != S[left]) {
                ret += S[left];
                ret += to_string(right - left);
                left = right;
            }
            if (right == n - 1) {
                ret += S[left];
                ret += to_string(right - left + 1);
            }
            right++;
        }

        if (ret.size() >= n)
            return S;
        return ret;
    }
};